package edu.xjtu.jih.demands;

import edu.xjtu.jih.common.Utils;
import org.apache.ibatis.session.SqlSession;

public class DemandsService {
    public boolean checkInsert(DemandsDto dto) {
        //System.out.println("6");

        SqlSession sqlSession = Utils.openSession();

        try{
            //System.out.println("2");
            //DemandsDao dao1 = null;
            //System.out.println("13");
            //sqlSession.getMapper();
            //System.out.println("12");
            DemandsDao dao = sqlSession.getMapper(DemandsDao.class);
            //System.out.println("3");
            dao.insertDemand(dto);
            //System.out.println("4");
            dao.insertUserDemand(dto);
            //System.out.println("5");

            sqlSession.commit();

            return true;
        }catch(Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally{
            sqlSession.close();
        }
    }

    public int selectMaxItemId() {
        SqlSession sqlSession = Utils.openSession();

        try{
            DemandsDao dao = sqlSession.getMapper(DemandsDao.class);
            sqlSession.commit();
            return  dao.selectItemId();
        }catch(Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally{
            sqlSession.close();
        }
    }
}
